package com.koron.css2.systemManage.service;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.common.utils.HttpUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.UserRangeBean;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataRangeService implements IDataRangeService{
	
	private static Logger log = LogManager.getLogger(DataRangeService.class);
	
	@Override
	public List<UserRangeBean> getUserRangeValueList(String userCode, String appId, String catalogue) {
		if(StringUtils.isEmpty(userCode)) {
			throw new IllegalArgumentException("_userCode 不能为空");
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantCode", ApplicationConfig.getAuthorityTenantCode());
		param.put("userCode", userCode);
		param.put("catalogue", catalogue);
		param.put("app", appId);
		String url = ApplicationConfig.getAuthorityUrl() + ApplicationConfig.GET_USER_RANGE;
		String result = HttpUtils.sendPostJson(url, JsonUtils.objectToJson(param));
		JSONObject json = JSONObject.fromObject(result);
		if(json.getInt("code") != 0) {
			log.error(json.getString("description"));
			throw new RuntimeException("查询角色列表失败");
		}
		return JsonUtils.jsonToList(json.getString("data"), UserRangeBean.class);
	}
	
	@Override
	public void addUserRangeValue(String appId, String userCode, String catalogue, List<String> valueList) {
		if(StringUtils.isEmpty(userCode)) {
			throw new IllegalArgumentException("userCode 不能为空");
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantCode", ApplicationConfig.getAuthorityTenantCode());
		param.put("userCode", userCode);
		param.put("app", appId);
		param.put("catalogue", catalogue);
		param.put("valueList", valueList);
		String url = ApplicationConfig.getAuthorityUrl() + "/permissionController/addUserRangeValue.htm";
		String result = HttpUtils.sendPostJson(url, JsonUtils.objectToJson(param));
		JSONObject json = JSONObject.fromObject(result);
		if(json.getInt("code") != 0) {
			log.error(json.getString("description"));
			throw new RuntimeException("添加用户数据域失败");
		}
	}
	
	@Override
	public void deleteUserRangeValue(String appId, String userCode, String catalogue, String value) {
		if(StringUtils.isEmpty(userCode)) {
			throw new IllegalArgumentException("userCode 不能为空");
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantCode", ApplicationConfig.getAuthorityTenantCode());
		param.put("userCode", userCode);
		param.put("app", appId);
		param.put("catalogue", catalogue);
		param.put("valueList", Arrays.asList(value));
		String url = ApplicationConfig.getAuthorityUrl() + "/permissionController/deleteUserRangeValue.htm";
		String result = HttpUtils.sendPostJson(url, JsonUtils.objectToJson(param));
		JSONObject json = JSONObject.fromObject(result);
		if(json.getInt("code") != 0) {
			log.error(json.getString("description"));
			throw new RuntimeException("删除用户数据域失败");
		}
	}
}
